package com.hengxinguotong.hxgtwygsp.ui.supervise.warning;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseTabFragment;
import com.hengxinguotong.hxgtwygsp.ui.supervise.patrol.info.PatrolInfoFragment;
import com.hengxinguotong.hxgtwygsp.ui.supervise.patrol.issue.PatrolIssueFragment;
import com.hengxinguotong.hxgtwygsp.ui.supervise.patrol.statistics.PatrolStatisticsFragment;
import com.hengxinguotong.hxgtwygsp.ui.supervise.warning.manager.WarningManagerFragment;
import com.hengxinguotong.hxgtwygsp.ui.supervise.warning.property.WarningPropertyFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class WarningFragment extends BaseTabFragment implements RadioGroup.OnCheckedChangeListener
{

    @BindView(R.id.warning_item_container)
    RadioGroup warningItemContainer;

    private List<Fragment> fragmentList = null;
    private FragmentManager fragmentManager = null;
    private FragmentTransaction fragmentTransaction = null;

    public WarningFragment()
    {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public int getLayoutResId()
    {
        return R.layout.fragment_warning;
    }

    @Override
    public void init()
    {
        warningItemContainer.setOnCheckedChangeListener(this);
        fragmentList = new ArrayList<>();
        fragmentManager = getChildFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        WarningManagerFragment warningManagerFragment = new WarningManagerFragment();
        fragmentTransaction.add(R.id.warning_frame, warningManagerFragment, "manager");
        fragmentTransaction.hide(warningManagerFragment);
        fragmentList.add(warningManagerFragment);
        WarningPropertyFragment warningPropertyFragment = new WarningPropertyFragment();
        fragmentTransaction.add(R.id.warning_frame, warningPropertyFragment, "property");
        fragmentList.add(warningPropertyFragment);
        fragmentTransaction.commit();
    }

    @OnClick({R.id.warning_property, R.id.warning_manager})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.warning_property:
                exchangePage(1);
                break;
            case R.id.warning_manager:
                exchangePage(0);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        RadioButton radioButton;
        for (int i = 0; i < group.getChildCount(); i++)
        {
            radioButton = ((RadioButton) group.getChildAt(i));
            radioButton.setTypeface(Typeface.defaultFromStyle(radioButton.getId() == checkedId ? Typeface.BOLD : Typeface.NORMAL));
        }
    }

    private void exchangePage(int index)
    {
        fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        for (int i = 0; i < fragmentList.size(); i++)
        {
            fragment = fragmentList.get(i);
            if (i == index)
            {
                fragmentTransaction.show(fragment);
            } else
            {
                fragmentTransaction.hide(fragment);
            }
        }
        fragmentTransaction.commitAllowingStateLoss();
    }
}
